<?php
require_once(substr(__FILE__, 0, strrpos(__FILE__, '/')) . "/inc/JSON.php");
require_once(substr(__FILE__, 0, strrpos(__FILE__, '/')) . "/inc/objects.php");

class Cleaner
{
  /**
   * Constructor
   */
  function Cleaner()
  {
    $this->path = substr(__FILE__, 0, strrpos(__FILE__, '/')) . "/";
    require_once($this->path . "Preferences.php");
    $this->Preferences = new Preferences();
  }
  
  function clean()
  {
    $lastClean = $this->Preferences->getLastClean();
    $now = time();
    if ($now > ($lastClean + 86399)) // Clean once per day
    {
      $path = $this->Preferences->getSchedulesFolder();
      if(file_exists($path))
      {
        $xmltvFolder = dir($path);
        
        $yesterday = date("Ymd",(time() - (24 * 60 * 60)));
        while(($filename = $xmltvFolder->read()) !== false)
        {
          // only remove files ending with .xml or .js and not beginning with a dot
          $xml = strpos($filename, ".xml");
          $js = strpos($filename, ".js");
          if(strpos($filename,".") > 0 && ($xml > 0 || $js > 0))
          {
            // Remove file only if it is older than yesterday
            $start = strpos($filename,"_")+1;
            if($js > 0)
            {
              $filedate = substr($filename,$start,$js-$start);
            }
            else
            {
              $filedate = substr($filename,$start,$xml-$start);
            }
            $filedate = str_replace("-","",$filedate);
            if($filedate < $yesterday)
            {
              unlink($path."/".$filename);
            }
          }
        }
        
        $xmltvFolder->close();
        $this->Preferences->setLastClean($now);
      }
    }
  }
}
?>